# _*_ coding:utf-8 _*_
from django.core.mail import EmailMultiAlternatives, EmailMessage
from django.template import loader
from Persimmon.celery import app
from Persimmon import settings

# """celery -A Persimmon worker -l debug """


@app.task
def send_email(to_email, send_type="register", template=None, **kwargs):
	subject = settings.DEFAULT_FROM_EMAIL,
	from_email = settings.EMAIL_HOST_USER
	if send_type == "register":
		# message = u"通知，你已成功注册了<烂土豆>，点击下列链接激活进入欢乐time"
		email_content = loader.render_to_string(
			template + '.html',
			kwargs
		)
		email_txt = loader.render_to_string(
			template + '.txt',
			kwargs  # 字典形式
		)
		# 两种格式文件发送
		email = EmailMultiAlternatives(subject, email_txt, from_email, [to_email])
		email.attach_alternative(email_content, mimetype='text/html')
		email.send(fail_silently=False)
	elif send_type == "reset_email":
		email_content = loader.render_to_string(
			template + '.html',
			kwargs
		)
		email = EmailMessage(subject,email_content,from_email,[to_email])
		email.content_subtype = "html"
		email.send(fail_silently=False)
